/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.npmpackage.core.service;

import com.inspur.edp.web.common.io.FileUtility;
import com.inspur.edp.web.common.logger.WebLogger;
import com.inspur.edp.web.npmpackage.api.entity.NpmInstallParameter;
import com.inspur.edp.web.npmpackage.api.entity.NpmPackageCheckUpdateOptions;
import com.inspur.edp.web.npmpackage.api.entity.NpmPackageResponse;
import com.inspur.edp.web.npmpackage.core.npminstall.NodeModulesPathGenerator;
import com.inspur.edp.web.npmpackage.core.npmpackagecheck.NpmPackageCheck;

/**
 * npm 包 manager service
 *
 * @author guozhiqi
 */
public class NpmPackageService {
    /**
     * 执行npm 版本检测
     *
     * @param checkUpdateOptions
     * @return
     */
    public static NpmPackageResponse checkUpdate(NpmPackageCheckUpdateOptions checkUpdateOptions) {

        // 升级工具中不进行检测
        if (checkUpdateOptions.isUpgradeTool()) {
            NpmPackageResponse npmPackageResponse = new NpmPackageResponse();
            npmPackageResponse.setSuccess(true);
            npmPackageResponse.setErrorMessage("升级工具执行，npm包版本不进行检测");
            return npmPackageResponse;
        }

        String currentServerPath = FileUtility.getCurrentWorkPath(checkUpdateOptions.isUpgradeTool());


        // 判断是否存在node_modules 避免被误删除的情况
        NpmInstallParameter npmInstallParameter = new NpmInstallParameter();
        npmInstallParameter.setExecuteEnvironment(checkUpdateOptions.getExecuteEnvironment());
        npmInstallParameter.setMobile(checkUpdateOptions.isMobile());
        String nodeModulesPath = NodeModulesPathGenerator.generatePackageJsonPath(npmInstallParameter, currentServerPath);
        nodeModulesPath = FileUtility.combine(nodeModulesPath, "node_modules");
        if (!FileUtility.exists(nodeModulesPath) || FileUtility.isEmptyFolder(nodeModulesPath)) {
            String tip = "未配置编译依赖node_modules,请在   Npm包管理进行在线安装。node_modules路径为:" + nodeModulesPath;
            WebLogger.Instance.info(tip, NpmPackageService.class.getName());
            return NpmPackageResponse.createError(tip);
        }

        // 是否需要update标识
        boolean npmCheckFlag = NpmPackageCheck.check(checkUpdateOptions);


        if (npmCheckFlag) {
            return NpmPackageResponse.create();
        } else {
            return NpmPackageResponse.createError("npm包版本存在更新，为保证最终脚本运行正确，需先进行更新。\r\n请在选项->设置->Npm包管理进行更新！");
        }
    }
}
